using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using System.Xml;
using System.Globalization;
using System.Threading;

namespace CrateManiaPipeline
{
    /// <summary>
    /// This class will be instantiated by the XNA Framework Content Pipeline
    /// to import a Tiled map editor file from disk into a <see cref="TileMapContent"/> instance.
    /// </summary>
    [ContentImporter(".tmx", DisplayName = "Tiled Map Importer", DefaultProcessor = "TileMapProcessor")]
    public class TileMapImporter : ContentImporter<TileMapContent>
    {
        public override TileMapContent Import(string filename, ContentImporterContext context)
        {
            XmlDocument xd = new XmlDocument();
            xd.Load(filename);

            CultureInfo previousCulture = Thread.CurrentThread.CurrentCulture;
            try
            {
                // ensures all parsing is done using "C" locale
                Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                return new TileMapContent(xd.SelectSingleNode("map"), context);
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = previousCulture;
            }
        }
    }
}
